<template>
  <el-dialog :append-to-body="true" title="配送时间设置" :visible.sync="visible" width="50%">
    <el-form ref="form" :model="modalTime">
      <el-form-item label="开始配送时间:" prop="deliveryStartTime" :rules="[{required: true, message: '选择开始配送时间', trigger: 'change'}]">
        <el-time-select v-model="modalTime.deliveryStartTime" :picker-options="{
          start: '08:00',
          step: '00:30',
          end: '24:00'
        }" placeholder="选择开始配送时间"></el-time-select>
      </el-form-item>
      <el-form-item label="结束配送时间:" prop="deliveryEndTime" :rules="[{required: true, message: '选择结束配送时间', trigger: 'change'}]">
        <el-time-select v-model="modalTime.deliveryEndTime" :picker-options="{
          start: '08:00',
          step: '00:30',
          end: '24:00'
        }" placeholder="选择结束配送时间"></el-time-select>
      </el-form-item>
      <el-form-item label="下单提前时间:" prop="orderAdvanceTime" style="margin-top: 15px" :rules="[{required: true, message: '下单提前时间', trigger: 'blur'}]">
        <el-input v-model="modalTime.orderAdvanceTime" size="small" min="0" style="width: 180px;" type="number">
        </el-input>分钟
      </el-form-item>
    </el-form>

    <div class="type-button">
      <el-button type="success" size="small" @click="editDeliveryTime">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      type: 'add',
      modalTime: { deliveryStartTime: null, deliveryEndTime: null, orderAdvanceTime: 180 } //配送时间
    };
  },
  methods: {
    show({ time, type }) {
      this.type = type
      this.visible = true
      if (type == 'add') {
        this.modalTime = { deliveryStartTime: null, deliveryEndTime: null, orderAdvanceTime: 180 } //配送时间
      } else {
        this.modalTime = time;
      }
    },
    editDeliveryTime() {
      this.$refs.form.validate(valid => {
        if(valid) {
          if (this.type == 'add') {
            this.$emit('addDeliveryItem', {
              deliveryStartTime: this.modalTime.deliveryStartTime + ':00',
              deliveryEndTime: this.modalTime.deliveryEndTime + ':00',
              orderAdvanceTime: this.modalTime.orderAdvanceTime,
            })
          }
          this.visible = false
        }
      })
      
    }
  }
};
</script>
<style lang="scss" scoped>
</style>